//
//  ComfirmOrderViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/1/17.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ComfirmOrderViewController.h"
#import "OrderCell1.h"
#import "OrderCell2.h"
#import "OrderCell3.h"
#import "OrderCell4.h"
#import "OrderCell5.h"
#import "SubSucViewController.h"
#import "OrderCreateViewController.h"
@interface ComfirmOrderViewController ()<UITableViewDataSource,UITableViewDelegate>{
    MBProgressHUD *hud;

    __weak IBOutlet UILabel *needPayLabel;
    NSMutableArray *array;
}
@property (weak, nonatomic) IBOutlet UITableView *orderTableView;
@property (strong,nonatomic) NSMutableDictionary *paramDic;
@property (strong,nonatomic) NSMutableDictionary *createOrderDic;
@end

@implementation ComfirmOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"确认订单";
    self.orderTableView.hidden=YES;
    _paramDic = [NSMutableDictionary dictionary];
    array = [NSMutableArray arrayWithObjects:@"-",@"-",@"-",@"-", nil];
    _createOrderDic = [NSMutableDictionary dictionary];
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    needPayLabel.text = @"总价：";
    [self.view addSubview:hud];
    // Do any additional setup after loading the view from its nib.
    [_orderTableView registerNib:[UINib nibWithNibName:@"OrderCell1" bundle:nil] forCellReuseIdentifier:@"OrderCell1"];
    [_orderTableView registerNib:[UINib nibWithNibName:@"OrderCell2" bundle:nil] forCellReuseIdentifier:@"OrderCell2"];
    [_orderTableView registerNib:[UINib nibWithNibName:@"OrderCell3" bundle:nil] forCellReuseIdentifier:@"OrderCell3"];
    [_orderTableView registerNib:[UINib nibWithNibName:@"OrderCell4" bundle:nil] forCellReuseIdentifier:@"OrderCell4"];
    [_orderTableView registerNib:[UINib nibWithNibName:@"OrderCell5" bundle:nil] forCellReuseIdentifier:@"OrderCell5"];
    
//    [self initorder];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    needPayLabel.text = [NSString stringWithFormat:@"总价： ¥%@",[_infoDic objectForKey:@"needPay"]];
    
    [array replaceObjectAtIndex:0 withObject:toStr([_infoDic objectForKey:@"needPay"])];
    [array replaceObjectAtIndex:1 withObject:toStr([_infoDic objectForKey:@"courseNum"])];
    [array replaceObjectAtIndex:2 withObject:toStr([_infoDic objectForKey:@"scheduleCost"])];
    [array replaceObjectAtIndex:3 withObject:toStr([_infoDic objectForKey:@"needPay"])];
    
    [_orderTableView reloadData];
    self.orderTableView.hidden=NO;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 5;
            break;
        case 2:
            return 0;
            break;
        case 3:
            return 1;
            break;
        case 4:
            return 0;
            break;
            
        default:
            return 0;
            break;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (0==section) {
        return nil;
    }
    UIView *tmp=[UIView new];
    tmp.backgroundColor = [UIColor clearColor];
    return tmp;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (0==section) {
        return 0;
    }
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return 44;
            break;
        case 1:{
            if (0==indexPath.row) {
                return 101;
            }else{
                return 44;
            }
        }
            break;
        case 2:
            return 44;
            break;
        case 3:
            return 50;
            break;
        case 4:
            return 44;
            break;
        case 5:
            return 44;
            break;
            
        default:
            return 0;
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{
            OrderCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell1"];
            NSString *phone = [_infoDic objectForKey:@"stuPhone"];
            if (!(phone&&[phone isEqual:[NSNull null]])) {
                if (phone.length>7) {
                    phone = [phone mutableCopy];
                    [(NSMutableString *)phone replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
                    cell.label.text = [NSString stringWithFormat:@"学生：%@ %@",[self.infoDic objectForKey:@"stuName"],phone];
                }else{
                    cell.label.text = [NSString stringWithFormat:@"学生：%@ %@",[self.infoDic objectForKey:@"stuName"],phone];
                }
            }else{
                cell.label.text = [NSString stringWithFormat:@"学生：%@",[self.infoDic objectForKey:@"stuName"]];
            }
            return cell;
        }
            break;
        case 1:{
            if (0==indexPath.row) {
                OrderCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell2"];
                cell.className.text = [_infoDic objectForKey:@"className"];
                cell.teachName.text = [_infoDic objectForKey:@"teachName"];
                [cell.teacherUrl sd_setImageWithURL:[_infoDic objectForKey:@"teacherUrl"] placeholderImage:[UIImage imageNamed:@"headportrait"]];
                cell.time.text=[_infoDic objectForKey:@"time"];
                cell.teacherName.text=[_infoDic objectForKey:@"teacherName"];
                return cell;
            }else{
                OrderCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell3"];
                switch (indexPath.row) {
                    case 1:
                        cell.item.text=_priceName;
                        break;
                    case 2:
                        cell.item.text=@"课次";
                        break;
                    case 3:
                        cell.item.text=@"课时费";
                        break;
                    case 4:
                        cell.item.text=@"课程总价";
                        break;
                        
                    default:
                        break;
                }
                if ([cell.item.text isEqualToString:@"课次"]) {
                    cell.count.text = [NSString stringWithFormat:@"%@",[array objectAtIndex:(indexPath.row-1)]];
                }else{
                    cell.count.text = [NSString stringWithFormat:@"¥%@",[array objectAtIndex:(indexPath.row-1)]];
                }
                
                return cell;
            }

        }
            break;
        case 2:{
            OrderCell4 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell4"];
            cell.type= (indexPath.row%2==0)?@"1":@"2";
            return cell;
        }
            break;
        case 3:{
            OrderCell5 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell5"];
            return cell;
        }
            break;
        case 4:{
            OrderCell5 *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell5"];
            return cell;
        }
            break;
            
        default:
            return nil;
            break;
    }
    return nil;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    if (2==indexPath.section) {
        return YES;
    }else{
        return NO;
    }
}
- (IBAction)payAction:(UIButton *)sender {
    [self createorder];
//    SubSucViewController *vc=[[SubSucViewController alloc]init];
//    vc.orderSn = @"";
//    vc.needPay = @"";
//    vc.issuscess = YES;
//    [self.navigationController pushViewController:vc animated:YES];
}

//-(void)initorder{
//    [hud showAnimated:YES];
//
//    [_paramDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"memberId"] forKey:@"memberId"];
//    [_paramDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"] forKey:@"stuId"];
//    [_paramDic setObject:_classId forKey:@"classId"];
//    [_paramDic setObject:_courseId forKey:@"courseId"];
//    
//    [[ZPNetworking sharedZPNetworking] POST:APIOrder@"/orders/initorder.json" parameters:_paramDic progress:nil success:^(id  _Nonnull responseObject) {
//        [hud hideAnimated:YES];
//        if (CodeIs200(responseObject)) {
//            NSDictionary *result = [responseObject objectForKey:@"result"];
//            _tableDic = [result objectForKey:@"result"];
//            
//            
//            needPayLabel.text = [NSString stringWithFormat:@"总价： ¥%@",[_tableDic objectForKey:@"needPay"]];
//            
//            [array replaceObjectAtIndex:0 withObject:toStr([_tableDic objectForKey:@"needPay"])];
//            [array replaceObjectAtIndex:1 withObject:toStr([_tableDic objectForKey:@"courseNum"])];
//            [array replaceObjectAtIndex:2 withObject:toStr([_tableDic objectForKey:@"scheduleCost"])];
//            [array replaceObjectAtIndex:3 withObject:toStr([_tableDic objectForKey:@"needPay"])];
//            
//            [_orderTableView reloadData];
//            self.orderTableView.hidden=NO;
////            [self getpaytype];
//        }else{
//            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//
//    } failure:^(NSError * _Nonnull error) {
//        [hud hideAnimated:YES];
//        [ZPPromptView showMessage:error.localizedDescription];
//        [self.navigationController popViewControllerAnimated:YES];
//    }];
//}

//-(void)getpaytype{
//    [hud showAnimated:YES];
//    [[ZPNetworking sharedZPNetworking] POST:APIOrder@"/orders/getpaytype.json" parameters:nil progress:nil success:^(id  _Nonnull responseObject) {
//        [hud hideAnimated:YES];
//        if (CodeIs200(responseObject)) {
//            NSDictionary *result = [responseObject objectForKey:@"result"];
//            
//        }else{
//            
//            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
//        }
//    } failure:^(NSError * _Nonnull error) {
//        [hud hideAnimated:YES];
//    }];
//}

-(void)createorder{
    [_paramDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"memberId"] forKey:@"memberId"];
    [_paramDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"] forKey:@"stuId"];
    [_createOrderDic setObject:_classId forKey:@"classId"];
    [_createOrderDic setObject:[_infoDic objectForKey:@"needPay"] forKey:@"needPay"];
    [_createOrderDic setObject:@"0" forKey:@"orderSource"];
    [_createOrderDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"memberId"] forKey:@"memberId"];
    [_createOrderDic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"] forKey:@"stuId"];
    
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] POST:APIOrder@"/orders/createorder.json" parameters:_createOrderDic progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            NSDictionary *result = [responseObject objectForKey:@"result"];

            OrderCreateViewController *vc=[[OrderCreateViewController alloc]init];
            vc.needPay = toStr([_infoDic objectForKey:@"needPay"]);
            vc.orderSn = toStr([result objectForKey:@"orderSn"]);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

@end
